<?php
// +----------------------------------------------------------------------
// | UCToo [ Universal Convergence Technology ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2020 https://www.uctoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Patrick <contact@uctoo.com>
// +----------------------------------------------------------------------
namespace app\admin\model\wechatopen;

use think\Model;
use traits\model\SoftDelete;

class Applet extends Model
{

    use SoftDelete;


    // 表名
    protected $name = 'wechatopen_applet';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'typedata_text',
        'status_text',
        'access_token_overtime_text',
        'ticket_overtime_text'
    ];

    const STATUS_UNAUTHORIZED = 'disable';
    const STATUS_AUTHORIZED = 'enable';

    const TYPE_MINI_PROGRAM = 1;
    const TYPE_OFFICIAL_ACCOUNT = 2;

    // 普通授权
    const AUTHORIZE_TYPE_NORMAL = 1;
    // 通过快速注册授权
    const AUTHORIZE_TYPE_REGISTER = 2;

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public static function getAppletConfig($appid){
        $applet = self::get(['appid'=>$appid]);
        $config = ['app_id'=>$applet['appid'], 'secret' =>$applet['appsecret'] , 'token'=> $applet['token'], 'aes_key' =>$applet['aeskey'], 'wechatapplet_id' =>$applet['wechatapplet_id']];  //建议系统内都以wechatapplet_id作为应用唯一标识
        $wechat_default = config('wechat.default') ? config('wechat.default') : [];
        $oauth_config = ['oauth' => [
            'scopes'   => ['snsapi_userinfo'],
            'callback' => 'admin/index/wxlogin_callback',
        ]];  //默认的公众号oauth配置，可在具体应用点赋值其他配置修改
        $config = array_merge($config,$wechat_default,$oauth_config);
        return $config;

    }

    public function getTypedataList()
    {
        return ['serv_account' => __('Typedata serv_account'), 'miniapp' => __('Typedata miniapp'), 'sub_account' => __('Typedata sub_account')];
    }

    public function getStatusList()
    {
        return ['enable' => __('Status enable'), 'disable' => __('Status disable'), 'unaudit' => __('Status unaudit')];
    }


    public function getTypedataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['typedata']) ? $data['typedata'] : '');
        $list = $this->getTypedataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAccessTokenOvertimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['access_token_overtime']) ? $data['access_token_overtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTicketOvertimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['ticket_overtime']) ? $data['ticket_overtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setAccessTokenOvertimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTicketOvertimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
